package com.lx.oms.controller.controller;

import java.math.BigInteger;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lx.oms.common.annotation.SysLog;
import com.lx.oms.common.controller.AbstractController;
import com.lx.oms.common.entity.Page;
import com.lx.oms.common.entity.R;
import com.lx.oms.common.utils.ImageOperation;
import com.lx.oms.controller.entity.BroadcastEntity;
import com.lx.oms.controller.service.BroadcastService;

/**
 * 首页轮播图供应
 *
 * @author houzhong
 * @date 2018年7月06日 下午1:55:51
 */
@RestController
@RequestMapping("/sys/broadcastrelease")
public class BroadcastReleaseController extends AbstractController {
	
	@Autowired
	private BroadcastService broadcastService;
	
	/**
	 * 列表
	 * @param params
	 * @return
	 */
	@RequestMapping("/list")
	public Page<BroadcastEntity> list(@RequestBody Map<String, Object> params) {
		return broadcastService.listBroadcast(params);
	}
		
	/**
	 * 新增
	 * @param tBroadcast
	 * @return
	 */
	@SysLog("新增轮播图")
	@RequestMapping("/save")
	public R save(@RequestBody BroadcastEntity broadcast,HttpServletRequest request) {
		//目录
		String url = request.getSession().getServletContext().getRealPath("/")+("/fileImage/");
		//解析base64位，压缩图片
		String yzmImagePath = ImageOperation.encode64Image(broadcast.getImg(),url);
		//获取图片路径
		broadcast.setImg(yzmImagePath);
		return broadcastService.saveBroadcast(broadcast);
	}
	
	/**
	 * 根据id查询详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/info")
	public R getById(@RequestBody String id) {
		BigInteger bId = new BigInteger(id);
		return broadcastService.getBroadcastById(bId);
	}
	
	/**
	 * 修改
	 * @param tBroadcast
	 * @return
	 */
	@SysLog("修改轮播图")
	@RequestMapping("/update")
	public R update(@RequestBody BroadcastEntity broadcast,HttpServletRequest request) {
		//目录
		String url = request.getSession().getServletContext().getRealPath("/")+("/fileImage/");
		//解析base64位，压缩图片
		String yzmImagePath = ImageOperation.encode64Image(broadcast.getImg(),url);
		//获取图片路径
		broadcast.setImg(yzmImagePath);
		return broadcastService.updateBroadcast(broadcast);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@SysLog("删除轮播图")
	@RequestMapping("/remove")
	public R batchRemove(@RequestBody String[] id) {
		return broadcastService.batchRemove(id);
	}
	
}
